# -*- coding:utf-8 -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
from datetime import datetime
import re

requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告

"""
剑鱼标讯
https://www.jianyu360.com/jylab/supsearch/index.html
"""
class JianYu():
    def __init__(self):
        #声明一个CookieJar对象实例来保存cookie
        self.cookie = cookiejar.CookieJar()
        ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        self.headers = {
            "Accept": "text/html, */*; q=0.01",
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Connection": "keep-alive",
            "Cookie": "SESSIONID=dc667a3a7d97a9d9810440ee85c332de10d497b8; SESSIONID=dc667a3a7d97a9d9810440ee85c332de10d497b8; UM_distinctid=16ed4b1b60f1d6-0a05a63212ccb-2393f61-144000-16ed4b1b610d1; CNZZDATA1261815924=761227703-1575524716-%7C1575524716; Hm_lvt_72331746d85dcac3dac65202d103e5d9=1575526447; Hm_lpvt_72331746d85dcac3dac65202d103e5d9=1575526447",
            "DNT": "1",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36",
        }


    def get_contents(self):
        postData = {

        }
        url = "https://www.jianyu360.com/jylab/supsearch/index.html"
        html = HttpUtils.do_request("GET", url, self.headers, postData)
        sub_str = re.findall(r"var list = (.*?)$", html.text, re.S|re.M)[0]
        data_json = json.loads(sub_str)
        for d in data_json:
            dict_data = dict()
            dict_data['id'] = d['id']  # id
            dict_data['area'] = d['area']  # 地区
            dict_data['subtype'] = d['subtype']  # 类型
            dict_data['title'] = d['title']  # 标题
            dict_data['timetemp'] = datetime.fromtimestamp(int(d['timetemp']))
            dict_data['industry'] = d['industry']  # 类别
            dict_data['buyer'] = d['buyer']  # 采购方
            dict_data['winner'] = d['winner']  # 供应方
            dict_data['projectcode'] = d['projectcode']  # 采购合同编号

            dict_data['url'] = f"https://www.jianyu360.com/article/content/{d['id']}.html?industry={d['industry']}"
            print(str(data_json))

    # 获取详细信息
    def get_contents(self, url):
        html = HttpUtils.do_request("GET", url, self.headers, "")


if __name__ == '__main__':
    jianyu = JianYu()
    jianyu.get_contents()